#include "admincourseform.h"
#include "ui_admincourseform.h"
#include <QDebug>

//constructor for admincourseform
AdminCourseForm::AdminCourseForm(QWidget *parent, AdminData *newData) :
    QWidget(parent),
    ui(new Ui::AdminCourseForm)
{
    ui->setupUi(this);

    this->data=newData;

    //will have persistably read in list of instructors to instructor vector
    //will make some samples for now:



    this->profList=(data->getProfs());
    //show previously created holidays

    int i=0;
    while(i<profList.size())
    {
        ui->listWidget_5->addItem(profList[i].getName());
        i++;
    }


    if(profList.isEmpty())
    {
    Professor *tba=new Professor("TBA");


    profList.append(*tba);



    ui->listWidget_5->addItem("TBA");
    }


    this->cList=(data->getCourses());
    //show previously created holidays


    int j=0;
    while(j<cList.size())
    {

        cList[j].setNumberOfSections(cList[j].getClassSection().size());

        int z=1;
        while(z<=cList[j].getNumberOfSections())
        {

            ClassSection *newClSect=new ClassSection(z);


            cList[j].addClassSection(*newClSect);
            z++;
        }



        ui->listWidget->addItem(cList[j].getCode());

        ui->comboBox->addItem(cList[j].getCode());

        ui->listWidget->setCurrentRow(j);

        j++;
    }

    ui->courseCodeLine->clear();
    ui->courseNameLine->clear();
    ui->listWidget_2->clear();

}






//destructor for admincourseform
AdminCourseForm::~AdminCourseForm()
{
    delete ui;
}


//button to go to outline section page
void AdminCourseForm::on_pushButton_clicked()
{
    AdminOutSectForm *aOSF=new AdminOutSectForm(this, data);
    aOSF->show();
    aOSF->topLevelWidget();
}


//button to go to calendar page
void AdminCourseForm::on_pushButton_2_clicked()
{
    AdminMain *aM=new AdminMain(this, data);
    aM->show();
    aM->topLevelWidget();

}



//remove course button
void AdminCourseForm::on_pushButton_4_clicked()
{
    //if course list is empty, give error message
    if(cList.isEmpty())
    {
        ui->courseCodeLine->clear();

        AdminErrorDialog *aed1=new AdminErrorDialog(this,"Nothing to remove.");
        aed1->setModal(true);
        aed1->exec();


    }
    else
    {//otherwise remove the item

        int y=cList.size();
        QString badCourse=cList[y-1].getCode();

        cList.remove(y - 1);

        ui->listWidget->takeItem(ui->listWidget->count() - 1);

        ui->comboBox->removeItem(y-1);


        //remove it as prereq and antireq for other courses
        int r=0;

        while(r<cList.size())
        {

            cList[r].removePrereq(badCourse);
            cList[r].removeAntireq(badCourse);


            r++;
        }


        this->on_listWidget_currentRowChanged(ui->listWidget->currentRow());



        //if no courses left, clear lists
        if(cList.isEmpty())
        {
            ui->listWidget_4->clear();
            ui->listWidget_2->clear();
            ui->listWidget_3->clear();
            ui->listWidget_6->clear();

        }

        //remove an item

    }
}


//save button (ie persistence goes here)
void AdminCourseForm::on_pushButton_5_clicked()
{
    data->setProfs(&profList);
    data->setCourse(&cList);

    AdminErrorDialog *aedPer2=new AdminErrorDialog(this,"Data has been saved.");
    aedPer2->setModal(true);
    aedPer2->exec();
}

//add prereq button
void AdminCourseForm::on_pushButton_6_clicked()
{


    //get which prereq is being set
    QString thisPrereq=ui->comboBox->currentText();

    int listInt=ui->listWidget->currentRow();
    if(listInt<0)
        listInt=0;

    //if no prerequistes, give error
    if(thisPrereq.isNull() || thisPrereq.isEmpty())
    {

        AdminErrorDialog *aed2=new AdminErrorDialog(this,"No courses added to choose from.");
        aed2->setModal(true);
        aed2->exec();


        //course cannot be a prereq for itself
    }else if(thisPrereq.compare(cList[listInt].getCode())==0)
    {


        AdminErrorDialog *aed3=new AdminErrorDialog(this,"Cannot set as prerequisite for itself.");
        aed3->setModal(true);
        aed3->exec();

    }
    else
    {
        //otherwise, check if it is already a prereq
        int i=0;
        bool isThere=false;
        while(i<cList[listInt].getPrereq().size()&& isThere==false)
        {
            if(cList[listInt].getPrereq()[i].getCode().compare(thisPrereq)==0)
            {
                isThere=true;
            }

            i++;
        }

        //check if it is already an antireq
        int k=0;
        bool isAnti=false;
        while(k<cList[listInt].getAntireq().size() && isAnti==false)
        {
            if(cList[listInt].getAntireq()[k].getCode().compare(thisPrereq)==0)
            {
                isAnti=true;
            }

            k++;
        }

        //if it is already a prereq, can't add it twice so give an error
        if(isThere==true)
        {

            AdminErrorDialog *aed4=new AdminErrorDialog(this,thisPrereq+" is already there.");
            aed4->setModal(true);
            aed4->exec();


            //if it is already an antireq, can't be both so give error
        }
        else if(isAnti==true)
        {
            AdminErrorDialog *aed5=new AdminErrorDialog(this,"A course cannot be an antireq and prereq for the same course.");
            aed5->setModal(true);
            aed5->exec();

        }
        else
        {

            //otherwise add the item
            ui->listWidget_3->addItem(thisPrereq);
            Course reqCourse=cList[ui->comboBox->currentIndex()];
            cList[listInt].addPrereq(reqCourse);
        }
    }
}


//remove prereq button
void AdminCourseForm::on_pushButton_9_clicked()
{
    //get which prereq is to be removed
    QString thisPrereq=ui->comboBox->currentText();

    int listInt=ui->listWidget->currentRow();
    if(listInt<0)
        listInt=0;


    //if no prereq available, can't remove any
    if(thisPrereq.isNull() || thisPrereq.isEmpty())
    {

        AdminErrorDialog *aed5=new AdminErrorDialog(this,"No courses added to remove.");
        aed5->setModal(true);
        aed5->exec();

        //otherwise
    }
    else
    {

        //check if prereq is there to remove
        int i=0;
        bool isThere=false;
        while(i<cList[listInt].getPrereq().size()&& isThere==false)
        {
            if(cList[listInt].getPrereq()[i].getCode().compare(thisPrereq)==0)
            {
                isThere=true;
            }

            i++;
        }


        //if it is not there, cannot delete it, so give error message
        if(isThere==false)
        {

            AdminErrorDialog *aed7=new AdminErrorDialog(this,thisPrereq+" is not there.");
            aed7->setModal(true);
            aed7->exec();
        }
        else
        {

            //if all is well, delete the prereq
            cList[listInt].removePrereq(thisPrereq);
            ui->listWidget_3->clear();
            int q=0;
            while(q<cList[listInt].getPrereq().size())
            {
                ui->listWidget_3->addItem(cList[listInt].getPrereq()[q].getCode());
                q++;
            }
        }
    }



}

//add antireq button
void AdminCourseForm::on_pushButton_7_clicked()
{

    //get which antireq is to be added
    QString thisAntireq=ui->comboBox->currentText();

    int listInt=ui->listWidget->currentRow();
    if(listInt<0)
        listInt=0;


    //if no requisites listed, no courses have been added, so give error message
    if(thisAntireq.isNull() || thisAntireq.isEmpty())
    {

        AdminErrorDialog *aed8=new AdminErrorDialog(this,"No courses added to choose from.");
        aed8->setModal(true);
        aed8->exec();

        //cannot set a course as it own antirequisite, so give error message
    }else if(thisAntireq.compare(cList[listInt].getCode())==0)
    {

        AdminErrorDialog *aed9=new AdminErrorDialog(this,"Cannot set as antireq for itself.");
        aed9->setModal(true);
        aed9->exec();

    }
    else
    {
        //check if course is already an antireq for the given course
        int i=0;
        bool isThere=false;
        while(i<cList[listInt].getAntireq().size()&& isThere==false)
        {
            if(cList[listInt].getAntireq()[i].getCode().compare(thisAntireq)==0)
            {
                isThere=true;
            }

            i++;
        }


        //check if it is already an antirequisite
        int k=0;
        bool isPre=false;
        while(k<cList[listInt].getPrereq().size() && isPre==false)
        {
            if(cList[listInt].getPrereq()[k].getCode().compare(thisAntireq)==0)
            {
                isPre=true;
            }

            k++;
        }


        //if it is already an antireq, give an error message
        if(isThere==true)
        {

            AdminErrorDialog *aed10=new AdminErrorDialog(this,thisAntireq+" is already there.");
            aed10->setModal(true);
            aed10->exec();
        }
        else if(isPre==true)
        {//if it is already a prerequisite, give an error message


            AdminErrorDialog *aed11=new AdminErrorDialog(this,"A course cannot be a prereq and antireq for the same course.");
            aed11->setModal(true);
            aed11->exec();
        }
        else
        {
            //otherwise, add the antirequisite
            ui->listWidget_6->addItem(thisAntireq);
            Course reqCourse=cList[ui->comboBox->currentIndex()];
            cList[listInt].addAntireq(reqCourse);
        }
    }


}

//remove antireq button
void AdminCourseForm::on_pushButton_10_clicked()
{
    //get which antireq is to be removed
    QString thisAntireq=ui->comboBox->currentText();

    int listInt=ui->listWidget->currentRow();
    if(listInt<0)
        listInt=0;

    //check if there are no courses available give error message
    if(thisAntireq.isNull() || thisAntireq.isEmpty())
    {

        AdminErrorDialog *aed12=new AdminErrorDialog(this,"No courses added to remove.");
        aed12->setModal(true);
        aed12->exec();


    }
    else
    {
        //check if the antireq is available to remove
        int i=0;
        bool isThere=false;
        while(i<cList[listInt].getAntireq().size()&& isThere==false)
        {
            if(cList[listInt].getAntireq()[i].getCode().compare(thisAntireq)==0)
            {
                isThere=true;
            }

            i++;
        }

        //if not there, give error message
        if(isThere==false)
        {

            AdminErrorDialog *aed15=new AdminErrorDialog(this,thisAntireq+" is not there.");
            aed15->setModal(true);
            aed15->exec();
        }
        else
        {

            //remove the antireq
            cList[listInt].removeAntireq(thisAntireq);
            ui->listWidget_6->clear();
            int q=0;
            while(q<cList[listInt].getAntireq().size())
            {
                ui->listWidget_6->addItem(cList[listInt].getAntireq()[q].getCode());
                q++;
            }
        }
    }
}



//assign instructor button
void AdminCourseForm::on_pushButton_8_clicked()
{
    //if no courses to assign to, give error message
    if(cList.isEmpty())
    {

        AdminErrorDialog *aed16=new AdminErrorDialog(this,"No courses added yet.");
        aed16->setModal(true);
        aed16->exec();
    }
    else
    {//otherwise

        //figure out which course, section, and instructor is chosen
        int courseIndex=ui->listWidget->currentRow();
        int sectIndex=ui->listWidget_4->currentRow();
        int instrIndex=ui->listWidget_5->currentRow();

        //just in case, want to avoid error
        if(sectIndex<0)
            sectIndex=0;
        if(instrIndex<0)
            instrIndex=0;
        if(courseIndex<0)
            courseIndex=0;

        //if list of profs or list of sections is empty, give error message
        if(profList.isEmpty() || cList[courseIndex].getClassSection().isEmpty())
        {

            AdminErrorDialog *aed17=new AdminErrorDialog(this,"Neither instructor list nor class section list can be empty.");
            aed17->setModal(true);
            aed17->exec();
        }
        else
        {//otherwise

            //show the instrcutor-section match
            QString putTogether= ui->listWidget_4->item(sectIndex)->text()+" -now taught by- "
                    +ui->listWidget_5->item(instrIndex)->text();

            ui->listWidget_2->addItem(putTogether);

            //add the prof the section
            Professor pp=profList[instrIndex];
            ClassSection cc = (cList[courseIndex].getClaSect()->at(sectIndex));
            cList[courseIndex].removeClassSection(sectIndex);
            cc.setProfessor(pp);
            cList[courseIndex].addClassSectionAt(cc, sectIndex);

            //cycle through the sections
            ui->listWidget_4->setCurrentRow((sectIndex+1)%(ui->listWidget_4->count()));

        }
    }
}



//add course
void AdminCourseForm::on_addCourseButton_clicked()
{
    //get the code, name, and number of sections
    QString cCode=ui->courseCodeLine->text();
    QString cNameLine=ui->courseNameLine->text();
    int cNumSects=ui->numSections->value();

    bool isGood=true;

    //if the code is null, give an error message
    if(cCode.isEmpty() || cCode.isNull())
    {

        AdminErrorDialog *aed18=new AdminErrorDialog(this,"Code can't be null.");
        aed18->setModal(true);
        aed18->exec();
        isGood=false;
    }
    //if the name is null, give an error message
    if(cNameLine.isEmpty() || cNameLine.isNull())
    {

        AdminErrorDialog *aed19=new AdminErrorDialog(this,"Name can't be null.");
        aed19->setModal(true);
        aed19->exec();
        isGood=false;
    }


    //check if the course is there already
    int i=0;
    bool isThere=false;
    while(i<cList.size()&& isThere==false)
    {
        if(cList[i].getCode().compare(cCode)==0)
        {
            isThere=true;
        }

        i++;
    }


    //if the course is there, give an error message
    if(isThere==true)
    {
        ui->courseCodeLine->clear();

        AdminErrorDialog *aed20=new AdminErrorDialog(this,cCode+" is already there.");
        aed20->setModal(true);
        aed20->exec();
        isGood=false;
    }

    //if all is well, add the course
    if(isGood==true)
    {


        Course *courseToAdd = new Course(cNameLine, cCode, cNumSects);
        ui->listWidget->addItem(courseToAdd->getCode());
        ui->comboBox->addItem(courseToAdd->getCode());
        ui->comboBox->setCurrentIndex(0);
        ui->courseCodeLine->clear();
        ui->courseNameLine->clear();
        ui->listWidget_2->clear();

        //now for sections, display and initialize all sections
        int j=1;
        ui->listWidget_4->clear();

        while(j<=cNumSects)
        {

            ClassSection *newClSect=new ClassSection(j);
            ui->listWidget_4->addItem(QString::number(j));

            courseToAdd->addClassSection(*newClSect);
            j++;
        }

        //add course
        cList.append(*courseToAdd);

        //update the listwidget row
        ui->listWidget->setCurrentRow((ui->listWidget->currentRow()+1)%(ui->listWidget->count()));
    }
}






//clicked an entry in course list thing
void AdminCourseForm::on_listWidget_currentRowChanged(int currentRow)
{

    //make sure the row is >0 to avoid errors
    if (currentRow >= 0)
    {

        //clear fields
        ui->courseCodeLine->clear();
        ui->courseNameLine->clear();


        //now for sections, display all sections for the course
        int j=1;
        ui->listWidget_4->clear();


        int cNumSects=cList[currentRow].getNumberOfSections();


        while(j<=cNumSects)
        {


            ui->listWidget_4->addItem(QString::number(j));

            j++;
        }


        //now for prereq and antireq stuff

        //clear fields
        ui->listWidget_3->clear();
        ui->listWidget_6->clear();

        //show the relevant prereqs
        int preSize=cList[currentRow].getPrereq().size();
        int p=0;
        while(p<preSize)
        {

            ui->listWidget_3->addItem(cList[currentRow].getPrereq()[p].getCode());
            p++;
        }

        //show the relevant antireqs
        int antSize=cList[currentRow].getAntireq().size();
        int a=0;
        while(a<antSize)
        {

            ui->listWidget_6->addItem(cList[currentRow].getAntireq()[a].getCode());
            a++;
        }

        //now for class sect-instructor link
        ui->listWidget_2->clear();



        //show how instructors and class sections link up
        int w=0;
        while(w<cNumSects)
        {


            if (cList[currentRow].getClassSection()[w].sectHasProf()==true)
            {

                ui->listWidget_2->addItem(QString::number(cList[currentRow].getClassSection()[w].getNumber())
                                          +" -now taught by- "+cList[currentRow].getClassSection()[w].getProfessor().getName());


            }

            w++;
        }
    }

}

void AdminCourseForm::on_pushButton_3_clicked()
{
    PrdfForm *PDF=new PrdfForm(this, data);
    PDF->show();
    PDF->topLevelWidget();
}



